package com.smartwasser.yunhtgl.apphtgl.utils;

import android.text.TextUtils;

import com.smartwasser.yunhtgl.apphtgl.App;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;

public class BaseUtils {

    public static int[] convertStrToNumber(String str) {
        int[] numbers = null;
        if (!TextUtils.isEmpty(str)) {
            int len = str.length();
            numbers = new int[len];
            try {
                for (int i = 0; i < len; i++) {
                    numbers[i] = Integer.parseInt(str.substring(i, i + 1));
                }
            } catch (Exception ex) {
            }
        }
        return numbers;
    }

    public final static String md5(String plaintext) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = plaintext.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static void removeValue(List<String> list, String id) {
        List<String> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (!(list.get(i).equals(id))) {
                list1.add(list.get(i));
            }
        }
        App.getInstance().getCaseProcess1List().clear();
        App.getInstance().getCaseProcess1List().addAll(list1);
    }

    public static File writeResponseBodyToDisk(String path, ResponseBody body) {
        Logger.e(body.contentLength() + "");
        Logger.e("writeResponseBodyToDisk");
        try {
            File futureStudioIconFile = new File(path);
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                byte[] fileReader = new byte[1024];
                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);
                while (true) {
                    int read = inputStream.read(fileReader);
                    if (read == -1) {
                        break;
                    }
                    outputStream.write(fileReader, 0, read);
                }
                outputStream.flush();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return futureStudioIconFile;
            } catch (IOException e) {
                return null;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return null;
        }
    }

    public static boolean isBCVisible(String task_key) {
        if (task_key.equals("zdhtqicao")) {
            return false;
        } else if (task_key.equals("dwfzr")) {
            return false;
        } else if (task_key.equals("ywzgbsfzr")) {
            return false;
        } else if (task_key.equals("htglgcs")) {
            return false;
        } else if (task_key.equals("fwbbmfzrsh")) {
            return true;
        } else if (task_key.equals("jtfgldsh")) {
            return true;
        } else if (task_key.equals("jtzjlsh")) {
            return true;
        } else if (task_key.equals("htxd1")) {
            return true;
        } else if (task_key.equals("fwbhtglgwbqr1")) {
            return true;
        } else if (task_key.equals("htxd2")) {
            return true;
        } else if (task_key.equals("htzgysc")) {
            return true;
        } else if (task_key.equals("fwbhtglgwbqr2")) {
            return true;
        } else if (task_key.equals("fwbhtglgsh")) {
            return true;
        }
        return false;
    }
}